package imsdk

import (
	"encoding/json"
	"longmen/server/config/global"
	"longmen/server/pkg/common/helper/http"
)

// AV群不支持这个操作
type ReCallRequest struct {
	GroupID    string   `json:"GroupId"` // 操作的群 ID
	MsgSeqList []MsgReq `json:"MsgSeqList"`
}
type MsgReq struct { // 被撤回的消息 seq 列表，一次请求最多可以撤回10条消息 seq
	MsgSeq int `json:"MsgSeq"` // 请求撤回的消息 seq
}

type ReCallResponse struct {
	ActionStatus  string `json:"ActionStatus"`
	ErrorInfo     string `json:"ErrorInfo"`
	ErrorCode     int    `json:"ErrorCode"`
	RecallRetList []struct {
		MsgSeq  int `json:"MsgSeq"`
		RetCode int `json:"RetCode"`
	} `json:"RecallRetList"`
}

// GroupMsgRecall 撤回消息
func (s sdk) GroupMsgRecall(req ReCallRequest) bool {
	reqUrl := s.BuildRestfulUrl("/group_open_http_svc/group_msg_recall")
	bytes, err := json.Marshal(req)
	if err != nil {
		global.Log.Error("request recall error: ", err)
		return false
	}
	result, err := http.HttpPost(reqUrl, string(bytes), connTimeoutMs, serveTimeoutMs, "application/json")
	resp := ReCallResponse{}
	err = json.Unmarshal(result, &resp)
	if err != nil {
		global.Log.Error("request recall Unmarshal error: ", err)
		return false
	}
	if resp.ActionStatus == FailActionStatus {
		global.Log.Error("request recall  error: ", err, "error message : ", resp.ErrorInfo)
		return false
	}
	return true
}
